package com.csair.platform.cit.service.biz.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csair.invoice.common.base.util.StringUtil;
import com.csair.invoice.common.base.util.collection.CollectionUtil;
import com.csair.platform.cit.facade.exception.CitBizException;
import com.csair.platform.cit.facade.model.CitDeclareRecordVo;
import com.csair.platform.cit.facade.model.CitSendParam;
import com.csair.platform.cit.facade.model.CitSendResultQueryParam;
import com.csair.platform.cit.service.biz.IDeclareResultService;
import com.csair.platform.cit.service.entity.CitDeclareRecord;
import com.csair.platform.cit.service.util.JsonHelper;
import com.google.gson.reflect.TypeToken;

import lombok.extern.slf4j.Slf4j;

/**
 * [企业所得税申报结果]服务实现类
 * @author ：caoyu
 * 
 */
@Slf4j
@Service
public class DeclareResultServiceImpl extends InitBaseServiceImpl implements IDeclareResultService {
	
	@Override
	public Page<CitDeclareRecordVo> findByPage(CitSendResultQueryParam param) {
		PageRequest pageable = PageRequest.of(param.getPageIndex() - 1, param.getPageSize(), new Sort(Direction.ASC, "id"));
		CitDeclareRecord q = new CitDeclareRecord();
        q.setNsrsbh(param.getNsrsbh());
        if(StringUtil.isNotEmpty(param.getSkssqq()) ){
			q.setSkssqq(param.getSkssqq());
		}
		if(StringUtil.isNotEmpty(param.getSkssqz())) {
			q.setSkssqz(param.getSkssqz());
		}

		if (StringUtil.isNotEmpty(param.getJmsbId())) {
			q.setJylsh(param.getJmsbId());
		}
		Example<CitDeclareRecord> ex = Example.of(q);
        Page<CitDeclareRecord> pages = declareRecordDao.findAll(ex, pageable);
        log.info("返回前端的[企业所得税-申报进度]数据为：" + pages.getContent().toString());		
        List<CitDeclareRecordVo> data = JsonHelper.from(pages.getContent(), new TypeToken<List<CitDeclareRecordVo>>() {
        }.getType());
        //查总条数
        long count = declareRecordDao.count(ex);
        Page<CitDeclareRecordVo> result = new PageImpl<>(data, pageable, count);
        return result;        
	}

	@Override
	public void deleteByListId(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new CitBizException(7001, "id不能是空值");
        }
		List<CitDeclareRecord> list = declareRecordDao.findAllByIds(ids);
		if (CollectionUtil.isNotEmpty(list)) {
			List<Long> deleteIds = new ArrayList<>();
			for (CitDeclareRecord entity : list) {
				String status = entity.getStatus();
				if (!"申报中".equals(status) && !"申报成功".equals(status)) {
					deleteIds.add(entity.getId());
				}
			}
			if (CollectionUtil.isNotEmpty(deleteIds)) {
				int count = deleteIds.size();
				declareRecordDao.deleteByIds(deleteIds);
				log.info("[企业所得税-申报进度]删除" + count + "行成功");
			}
		}
	}

	@Override
	public CitDeclareRecord findSuccessDeclareRecord(CitSendParam param) {

		CitDeclareRecord citDeclareRecord = declareRecordDao.findByTaxNoAndSkssAndStatusSuccess(param.getNsrsbh(), param.getSkssqq(), param.getSkssqz());
		return citDeclareRecord;
	}

	@Override
	public CitDeclareRecordVo getById(Long id) {
		 CitDeclareRecord declareRecord = declareRecordDao.findById(id).get();
		 return (CitDeclareRecordVo) JsonHelper.from(declareRecord,CitDeclareRecordVo.class);
	}
}
